// region:    --- Modules

mod error;

pub use self::error::{Error, Result};
use crate::model::db_enums::UserRole;

// endregion: --- Modules

#[cfg_attr(feature = "with-rpc", derive(rpc_router::RpcResource))]
#[derive(Clone, Debug)]
pub struct Ctx {
    user_id: i64,
    role: UserRole,
}

// Constructors.
impl Ctx {
    pub fn root_ctx() -> Self {
        Ctx {
            user_id: 0,
            role: UserRole::Admin,
        }
    }

    pub fn new(user_id: i64, role: UserRole) -> Result<Self> {
        if user_id == 0 {
            Err(Error::CtxCannotNewRootCtx)
        } else {
            Ok(Self { user_id, role })
        }
    }
}

// Property Accessors.
impl Ctx {
    pub fn user_id(&self) -> i64 {
        self.user_id
    }

    pub fn role(&self) -> UserRole {
        self.role.clone()
    }
}
